import 'package:furaha_mkopo/commonTool/fm_jump_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_report_event_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_route_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_time_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_progress_hud.dart';
import 'package:furaha_mkopo/commonUI/fm_toast.dart';
import 'package:furaha_mkopo/manager/fm_global_manager.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_click_access_api.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_detail_api.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_ought_api.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_click_access_entity.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_detail_entity.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_ought_entity.dart';
import 'package:furaha_mkopo/page/login/fm_login_page.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';

class FMHomeBigController extends GetxController{

  FMHomeDetailEntity? detailEntity;
  Position? position;
  int startTime = 0;

  @override
  void onInit() {
    super.onInit();
    startTime = FMTimeTool.currentTime;
  }

  void onRequestClickAccess(String useless,Function(String tag,String? bidding,String curListen) requestCallback) {
    startTime = FMTimeTool.currentTime;
    FMProgressHud.showLoading();
    FMHomeClickAccessApi api = FMHomeClickAccessApi(useless: useless);
    api.send<FMHomeClickAccessEntity, FMHomeClickAccessEntity>(decodeType: FMHomeClickAccessEntity()).then((value) {
      FMProgressHud.hideLoading();
      if (value.code == -2) {
        FMRouteTool.push(FMGlobalManager().context!, const FMLoginPage());
        return;
      }
      if(value.data == null){
        FMToast.showText(value.message ?? "Network request error");
        return;
      }
      String url = value.data?.bidding ?? "";
      if(url.startsWith("http")){
        FMJumpTool.jumpWidget(url: url);
      }else {
        onRequestNoEmptyListen(useless, requestCallback);
      }
    });
  }

  void onRequestNoEmptyListen(String listen,Function(String tag,String? bidding,String curListen) requestCallback){
    FMProgressHud.showLoading();
    FMHomeDetailApi api = FMHomeDetailApi(listen);
    api
        .send<FMHomeDetailEntity, FMHomeDetailEntity>(
        decodeType: FMHomeDetailEntity())
        .then((value) {
      FMProgressHud.hideLoading();
      if (value.data == null) {
        FMToast.showText(value.message ?? "Network request error");
        return;
      }
      detailEntity = value.data;
      String tag = detailEntity?.declared?.hunt??"";
      String bidding = detailEntity?.declared?.bidding??"";
      requestCallback(tag,bidding,listen);
    });
  }

  void onRequestOught(Function(String bidding) onCompleted){
    String adopt = detailEntity?.masters?.adopt ??"";
    String attitude = detailEntity?.masters?.attitude ??"";
    String affair = detailEntity?.masters?.affair ??"";
    String projects = detailEntity?.masters?.projects.toString() ??"";
    FMProgressHud.showLoading();
    FMHomeOughtApi oughtApi = FMHomeOughtApi(adopt, attitude, affair, projects);
    oughtApi.send<FMHomeOughtEntity,FMHomeOughtEntity>(decodeType: FMHomeOughtEntity()).then((value){
      FMProgressHud.hideLoading();
      if(value.code == null || value.code != 0){
        FMToast.showText(value.message ?? "Network request error");
        return;
      }
      reportRisk("9");
      onCompleted(value.data?.bidding??'');
    });
  }

  void reportRisk(String type) {
    int endTime = FMTimeTool.currentTime;
    FMReportEventTool.reportRisk(type, position?.latitude, position?.longitude, endTime, endTime);
  }
}